<?php
/**
 * 分类管理
 */
Class CategoryAction extends CommonAction{
    
    //分类列表视图
    Public function index(){
        import('Class.Category',APP_PATH);
        $cate = M('cate') ->order('sort ASC') -> select();
        //$cate = Category::getChilds($cate,5);
       // $cate = Category::getParents($cate,13);
//        p($cate);
        //$cate = Category::unlimitedForLayer($cate);
//        p($cate);die;
        $cate = Category::unlimitedForLevel($cate,'&nbsp;&nbsp;--');
        $this -> cate = $cate;
        $this -> display();
    }
    
    //添加分类视图
    Public function addCate(){
        //$pid = isset($_GET['pid']) ? $_GET['pid'] :0;等同于下面的方法$pid = I('pid',0,'intval')
        $this -> pid = I('pid',0,'intval');
        $this -> display();
    }
    
    //添加分类表单处理
    Public function runAddCate(){
        if(M('cate') -> add($_POST)){
            $this -> success('添加成功',U(GROUP_NAME.'/Category/index'));
        }else{
            $this -> error('添加失败');
        }
    }
    
    //排序
    Public function sortCate(){
        $db = M('cate');
        $db ->  getField();
        foreach($_POST AS $id => $sort){
            $db -> where(array('id' => $id)) -> setField('sort', $sort);
        }
        $this -> redirect(GROUP_NAME.'/Category/index');
    }
}